package com.cy.ppj.service.mall.impl;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.mall.StoreMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.mall.Store;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.StoreAdminListQuery;
import com.cy.ppj.model.vo.mall.StoreAdminListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.mall.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 门店 service
 * @Author tanyilin
 * @Date 2019/7/2 17:08
 */
@Service
public class StoreServiceImpl extends BaseService implements StoreService {

    @Autowired
    private StoreMapper storeMapper;


    @Override
    public PageBean<StoreAdminListVO> getStoreAdminList(StoreAdminListQuery query) {

        int total = storeMapper.selectStoreAdminListCount(query);
        List<StoreAdminListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = storeMapper.selectStoreAdminList(query);
            list.stream().forEach(s -> {
                s.setStatusText(s.getStatus().getText2());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    @Transactional
    public void addStore(Store insertStore) {

        Date time = new Date();
        insertStore.setWeight(storeMapper.selectMaxWeight() + 1);
        insertStore.setStatus(CommonStatus.ENABLE);
        insertStore.setCreateTime(time);
        insertStore.setUpdateTime(time);
        storeMapper.insertSelective(insertStore);

    }

    @Override
    @Transactional
    public void updateStore(Store updateStore) {

        Date time = new Date();
        updateStore.setUpdateTime(time);
        storeMapper.updateByPrimaryKeySelective(updateStore);
    }
}
